#!/usr/bin/env python3

import argparse
import os
import subprocess
import sys
from email.utils import parseaddr

sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))

from scripts.lib.setup_path import setup_path

setup_path()

os.environ["DJANGO_SETTINGS_MODULE"] = "zproject.settings"

import django

django.setup()
from django.conf import settings

parser = argparse.ArgumentParser()
parser.add_argument(
    "--no-relative-links",
    action="store_false",
    dest="show_relative_links",
    help="Disable relative links when using NavigationSteps component. Typically only set to true in case of the help center being hosted on zulip.com.",
)

parser.add_argument(
    "--out-dir",
    action="store",
    dest="out_dir",
    help="Output directory for the final build.",
)
args = parser.parse_args()


def run() -> None:
    env = os.environ.copy()
    env["SUPPORT_EMAIL"] = parseaddr(settings.ZULIP_ADMINISTRATOR)[1]
    env["CORPORATE_ENABLED"] = str(settings.CORPORATE_ENABLED).lower()
    env["SHOW_RELATIVE_LINKS"] = str(args.show_relative_links).lower()
    build_command = ["/usr/local/bin/corepack", "pnpm", "build"]
    if args.out_dir:
        build_command += ["--outDir", args.out_dir]

    subprocess.check_call(
        build_command,
        cwd="starlight_help",
        env=env,
    )


run()
